const express = require("express");
const cookieParser = require("cookie-parser");
const favicon = require("serve-favicon");
const morgan = require("morgan");
const path = require("path");
const template = require("./middleware/template.js");
module.exports = async function (app,config) {
  const { logger, APP_PATH, cookieKey, maxAge, JSON_LIMIT, appName, version } =
    config;
  //日志
  app.use(morgan(logger.level));

  // favicon 图标
  app.use(favicon(path.join(APP_PATH, "public/favicon.ico")));

  //cookie
  app.use(cookieParser(cookieKey));

  //解析接口 json & url
  app.use(express.json({ limit: JSON_LIMIT }));
  app.use(express.urlencoded({ extended: false }));

  //配置模板引擎
  template(app,config);

  //使用静态资源 ,
  app.use(
    "/public",
    express.static(path.join(APP_PATH, "public"), {
      maxAge: maxAge,
    })
  );

  //设置头信息
  app.use((req, res, next) => {
    res.setHeader("Create-By", "Chanjs");
    res.setHeader("X-Powered-By", appName);
    res.setHeader(appName, version);
    next();
  });
};
